package com.longxi.tianjintianbao.ui.fragment.repair;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.activity.repair.RepairListActivity;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.ui.presenter.repair.RepairListFgPresenter;
import com.longxi.tianjintianbao.ui.view.repair.IRepairListFgView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

public class RepairListFragment extends LazyFragment<IRepairListFgView, RepairListFgPresenter> implements IRepairListFgView {

    private String state = "-1";
    private String buildId = "";

    private int page = 0;

    @Override
    protected int provideContentViewId() {
        return R.layout.include_general_refresh_list;
    }

    @Override
    protected void init(View view) {
        //初始化View
        initView();
        mPresenter.initAdapter();

        //初始化数据
        initData();
    }

    public void initView(){

    }

    @Override
    protected RepairListFgPresenter createPresenter() {
        return new RepairListFgPresenter((RepairListActivity) getActivity(), this);
    }

    private void initData() {
        state = getArguments().getString("state");
        buildId = getArguments().getString("buildId");
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.getRepairList(page = 0, state, buildId);
            }

            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getRepairList(page, state, buildId);
            }
        });
        mPresenter.getRepairList(page = 0, state, buildId);
    }

    public RepairListFragment setArguments(String state, String buildId) {
        Bundle bundle = new Bundle();
        bundle.putString("state", state);
        bundle.putString("buildId", buildId);
        setArguments(bundle);
        return this;
    }

    @Override
    public SmartRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    @Override
    public RecyclerView getRvContent() {
        return mRvContent;
    }

    public void refreshReceiptList() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void onDestroy() {
        mPresenter.onDestroy();
        super.onDestroy();
    }
}
